package com.junmo.wechat.pojo.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.junmo.common.base.BaseEntity;
import com.junmo.common.constant.GlobalConstants;
import com.junmo.common.util.StringUtils;
import com.junmo.common.util.oConvertUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 小程序会员实体
 */
@Data
@NoArgsConstructor
@TableName("tb_sys_member")
public class SysMember extends BaseEntity {

    @TableId(type = IdType.INPUT)
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "主键,自增")
    private Long id;

    @ApiModelProperty(value = "昵称")
    private String nickName;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "真名")
    private String realName;

    @ApiModelProperty(value = "手机号")
    private String mobile;

    @ApiModelProperty(value = "邮箱")
    private String email;

    @ApiModelProperty(value = "sessionKey")
    private String sessionKey;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "生日")
    private LocalDate birthday;

    @ApiModelProperty(value = "头像")
    private String avatarUrl;

    @ApiModelProperty(value = "微信openId")
    private String openid;

    @ApiModelProperty(value = "公司")
    private String company;

    @ApiModelProperty(value = "账户状态")
    private Integer status;

    @ApiModelProperty(value = "性别 1:男 2:女")
    private Integer sex;

    @ApiModelProperty(value = "md5密码盐")
    private String salt;

    @ApiModelProperty(value = "用户来源：1:微信小程序 9:其他")
    private Integer sourceType;

    @ApiModelProperty(value = "创建日期")
    @TableField(fill = FieldFill.INSERT)
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreate;

    /**
     * 初始化用户信息，针对部分字段进行默认赋值
     */
    public void initMember() {
        if (this.salt == null) {
            this.salt = oConvertUtils.randomGen(8);
        }
        if (this.gmtCreate == null) {
            this.gmtCreate = LocalDateTime.now();
        }
        if (this.sourceType == null) {
            this.sourceType = GlobalConstants.SOURCE_TYPE_OTHER;
        }
        if (StringUtils.isEmpty(this.nickName)) {
            this.nickName = "游客" + oConvertUtils.randomNumber(10);
        }
    }
}
